package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.*;
import com.alibaba.csp.sentinel.dashboard.rule.AbstractDynamicRuleProviderAndPublisher;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleConstants;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
@ConfigurationProperties(prefix="sentinel.datasource.nacos")
public class NacosConfig {
    public static final String GROUP_ID = "SENTINEL_GROUP";

    private String serverAddr = "localhost:8848";
    private String namespace = "";
    
    @Bean
    public ConfigService nacosConfigService() throws Exception {
        Properties properties = new Properties();
        properties.put("serverAddr", serverAddr);
        properties.put(PropertyKeyConst.NAMESPACE, namespace);
        return ConfigFactory.createConfigService(properties);
    }
    
    @Bean
    @ConditionalOnProperty(prefix=DynamicRuleConstants.PROPERTY_PREFIX, 
    name=DynamicRuleConstants.AUTHORITY_STORE, havingValue=DynamicRuleConstants.SUPPORT_NACOS)
    public AbstractDynamicRuleProviderAndPublisher<AuthorityRuleEntity> authorityRuleProviderAndPublisher() {
        return new AbstractNacosRuleProviderAndPublisher<AuthorityRuleEntity>() {
            @Override
            protected String ruleSuffix()
            {
                return DynamicRuleConstants.AUTHORITY_DATA_ID_POSTFIX;
            }
        };
    }
    
    @Bean
    @ConditionalOnProperty(prefix=DynamicRuleConstants.PROPERTY_PREFIX, 
    name=DynamicRuleConstants.DEGRADE_STORE, havingValue=DynamicRuleConstants.SUPPORT_NACOS)
    public AbstractDynamicRuleProviderAndPublisher<DegradeRuleEntity> degradeRuleProviderAndPublisher() {
        return new AbstractNacosRuleProviderAndPublisher<DegradeRuleEntity>() {
            @Override
            protected String ruleSuffix()
            {
                return DynamicRuleConstants.DEGRADE_DATA_ID_POSTFIX;
            }
        };
    }
    
    @Bean
    @ConditionalOnProperty(prefix=DynamicRuleConstants.PROPERTY_PREFIX, 
    name=DynamicRuleConstants.FLOW_STORE, havingValue=DynamicRuleConstants.SUPPORT_NACOS)
    public AbstractDynamicRuleProviderAndPublisher<FlowRuleEntity> flowRuleProviderAndPublisher() {
        return new AbstractNacosRuleProviderAndPublisher<FlowRuleEntity>() {
            @Override
            protected String ruleSuffix()
            {
                return DynamicRuleConstants.FLOW_DATA_ID_POSTFIX;
            }
        };
    }
    
    @Bean
    @ConditionalOnProperty(prefix=DynamicRuleConstants.PROPERTY_PREFIX, 
    name=DynamicRuleConstants.PARAM_STORE, havingValue=DynamicRuleConstants.SUPPORT_NACOS)
    public AbstractDynamicRuleProviderAndPublisher<ParamFlowRuleEntity> paramRuleProviderAndPublisher() {
        return new AbstractNacosRuleProviderAndPublisher<ParamFlowRuleEntity>() {
            @Override
            protected String ruleSuffix()
            {
                return DynamicRuleConstants.PARAM_FLOW_DATA_ID_POSTFIX;
            }
        };
    }
    
    @Bean
    @ConditionalOnProperty(prefix=DynamicRuleConstants.PROPERTY_PREFIX, 
    name=DynamicRuleConstants.SYSTEM_STORE, havingValue=DynamicRuleConstants.SUPPORT_NACOS)
    public AbstractDynamicRuleProviderAndPublisher<SystemRuleEntity> systemRuleProviderAndPublisher() {
        return new AbstractNacosRuleProviderAndPublisher<SystemRuleEntity>() {
            @Override
            protected String ruleSuffix()
            {
                return DynamicRuleConstants.SYSTEM_DATA_ID_POSTFIX;
            }
        };
    }
    

    public String getServerAddr()
    {
        return serverAddr;
    }

    public void setServerAddr(String serverAddr)
    {
        this.serverAddr = serverAddr;
    }

    public String getNamespace()
    {
        return namespace;
    }

    public void setNamespace(String namespace)
    {
        this.namespace = namespace;
    }

}
